package com.server.start;

import com.register.client.domian.RegisterRequest;
import com.register.client.domian.Response;
import com.server.entity.Registry;
import com.server.entity.ServiceInstance;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * client提交任务
 */
@RestController
@RequestMapping("/submit")
public class CaptainServerSubmit {


    /**
     * client端服务下线
     */
    @RequestMapping("/extirpateInstance")
    public Response extirpateInstance(@RequestBody RegisterRequest registerRequest) {
        //1.
        Registry instance = Registry.getInstance();
        Map<String, Map<String, ServiceInstance>> registry = instance.getRegistry();
        if (registry != null) {
            Map<String, ServiceInstance> serviceInstanceMap = registry.get(registerRequest.getServiceName());
            if (serviceInstanceMap.values().size() > 0) {
                instance.remove(registerRequest.getServiceName(),registerRequest.getServiceInstanceId());
            }
            //如果服务没有实例了,删除注册表中的信息
            if (serviceInstanceMap.size() == 0) {
                registry.remove(registerRequest.getServiceName());
            }
        }
        return Response.SUCCESS;
    }

}
